package com.example.demo.alipay.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.demo.alipay.config.AlipayProperties;
import com.example.demo.alipay.pojo.ItemDetails;

/**
 * 
 * <p>
 * 功能描述： pc 端 网站支付
 * </p>
 * 
 * @author lixh
 * @date 2019年3月15日 上午10:19:16
 * @version 1.0
 */

@Controller
@RequestMapping("/alipay/page")
public class AlipayPagePayController {

	@Autowired
	private AlipayProperties alipayProperties;

	@Autowired
	private AlipayClient alipayClient;

	@Autowired
	private AlipayController alipayController;

	@PostMapping("/gotoPayPage")
	@ResponseBody
	public String gotoPayPage(ItemDetails details, HttpServletResponse response)
			throws AlipayApiException, IOException {
		JSONObject object = new JSONObject();
		// 快速即时交易支付
		String productCode = "FAST_INSTANT_TRADE_PAY";
		AlipayTradePagePayModel model = new AlipayTradePagePayModel();

		model.setOutTradeNo(details.getCommodityNo());
		model.setSubject(details.getCommodityName());
		model.setTotalAmount(details.getMoney());
		model.setBody(details.getCommodityDetail());
		model.setProductCode(productCode);

		AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
		// TODO 正式环境替换URL
		request.setReturnUrl("http://localhost:8089/alipay/page/returnUrl");
		request.setNotifyUrl(alipayProperties.getNotifyUrl());
		request.setBizModel(model);

		// 调用SDK生成表单，并直接输出到页面
		String form = alipayClient.pageExecute(request).getBody();
		// response.setContentType("text/html;charset=" +
		// alipayProperties.getCharset());
		object.put("html", form.toString());
		return object.toJSONString();
		// PrintWriter writer = response.getWriter();
		// writer.write(form);
		// writer.flush();
		// writer.close();
	}
	/**
	 * 
	 * 方法描述: 支付之后，返回的页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws AlipayApiException
	 * @author lixh
	 */
	@RequestMapping("returnUrl")
	public String returnUrl(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, AlipayApiException {
		response.setContentType("text/html;charset=" + alipayProperties.getCharset());
		boolean result = alipayController.rsaCheckV1(request);
		if (result) {
			// 验证成功
			// 请在这里加上商户的业务逻辑程序代码，如保存支付宝交易号
			// TODO
			// 商户订单号
			String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
			// 支付宝交易号
			String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
			System.out.println("商户订单号--->" + out_trade_no + "支付宝交易号---->" + trade_no);
			return "pagePaySuccess";
		}
		return "pagePayFail";
	}

}
